#!/bin/bash
#################################################################
# @Description: 用例名称：支持三种CPU型号模拟模式，测试目的：支持三种CPU型号模拟模式
# @Date : 2023-11-24
# @Last Modified by :
# @Last Modified time: 2023-11-24
#################################################################
source "../common.sh"
export LANG=en_US.UTF-8
res=0
vm6_name="vm6"
vm7_name="vm7"
vm8_name="vm8"
vm6_xml="/opt/${vm6_name}.xml"
vm7_xml="/opt/${vm7_name}.xml"
vm8_xml="/opt/${vm8_name}.xml"
arch=$(arch)
cleanup_environment >/dev/null

# 逐个启动vm6，vm7，vm8虚拟机，验证配置参数是否能正常启动
[ -e "$vm6_xml" ] && echo "已准备好测试xml" || { echo "请确认是否准备好xml"; exit 1; }
echo "1. 直通模式虚拟机启动测试开始，虚拟机vm6关键配置如下："
cat $vm6_xml|grep "<cpu mode='host-passthrough' check='none'>" || res=1
cat $vm6_xml|grep "<topology sockets='2' dies='1' cores='2' threads='1'/>" || res=1

echo "开始执行命令：virsh define $vm6_name"
virsh define "$vm6_xml" > /dev/null 2>&1 || { res=1; echo "$vm6_name define failed"; exit 1; }
echo "开始执行命令：virsh start $vm6_name"
virsh start "$vm6_name" > /dev/null 2>&1 || { res=1; echo "$vm6_name start failed"; exit 1; }
# 循环查询虚拟机的 IP 地址
echo "虚拟机vm6 启动中······"
vnc_port6=$(virsh vncdisplay $vm6_name)
while true; do
    ip_vm6=$(get_vm_ip "$vm6_name")
    # 如果有 IP 地址，打印信息并写入文件
    if [ -n "$ip_vm6" ]; then
        check_and_ping $ip_vm6 > /dev/null && echo "虚拟机$vm6_name启动正常！"  || { ret=1; echo '虚拟机$vm6_name 启动失败'; }
        break  # 退出循环
    fi
    sleep 10
done
host_cpu_flag=$(lscpu|grep Flags|awk -F: '{print $2}'|awk '{gsub(/^\s+|\s+$/, "");print}'| awk -F ' ' '{print $1}') 
sshpass -p "$password" ssh -o StrictHostKeyChecking=no -q $ip_vm6 "lscpu" >cpu_info
guest6_cpu_flag=$(grep Flags cpu_info|awk -F: '{print $2}'|awk '{gsub(/^\s+|\s+$/, "");print}' | awk -F ' ' '{print $1}')
echo "物理机CPU指令集包含：${host_cpu_flag}"
echo "虚拟机VM6 CPU指令集包含：${guest6_cpu_flag}"
[ "$host_cpu_flag" == "$guest6_cpu_flag" ] && echo "检查host-passthrough虚拟机CPU flag与主机一致" || { echo "检查host-passthrough虚拟机CPU flag与主机不一致"；res=1;}
# 输出最终状态
[ "$res" = "0" ] && echo -e "\033[32m-----------------1.直通模式配置完成虚拟机$vm6_name能够正常启动，虚拟机CPU类型与物理机CPU类型一致----------------\033[0m"
echo -e "\n\n\n"

cleanup_environment /dev/null 2>&1 
[ -e "$vm7_xml" ] && echo "已准备好测试xml" || { echo "请确认是否准备好xml"; exit 1; }
echo "2. 宿主模式虚拟机启动测试开始，虚拟机vm7关键配置如下："
cat $vm7_xml|grep "<cpu mode='host-model'>" || res=1
cat $vm7_xml|grep "<model fallback=\"allow\"/>" || res=1
cat $vm7_xml|grep "<topology sockets='2' dies='1' cores='2' threads='1'/>" || res=1

echo "开始执行命令：virsh define $vm7_xml"
virsh define $vm7_xml  > /dev/null 2>&1 || { res=1; echo "$vm7_name define failed"; exit 1; }
echo "开始执行命令：virsh start $vm7_xml"
virsh start "$vm7_name" > /dev/null 2>&1 || { res=1; echo "$vm7_name start failed"; exit 1; }
if [ $? -ne 0 ]; then
  echo "vm7 start failed."
fi
vnc_port7=$(virsh vncdisplay $vm7_name)
# 循环查询虚拟机的 IP 地址
echo "虚拟机vm7 启动中······"
while true; do
    ip_vm7=$(get_vm_ip "$vm7_name")
    # 如果有 IP 地址，打印信息并写入文件
    if [ -n "$ip_vm7" ]; then
        check_and_ping $ip_vm7 > /dev/null && echo "虚拟机$vm7_name 启动正常！"  || { res=1; echo '虚拟机$vm7_name 启动失败'; }
        break  # 退出循环
    fi
    sleep 10
done
if [ "${arch}" == "x86_64" ];then
	guest7_cpu_model=$(sshpass -p "$password" ssh -o StrictHostKeyChecking=no -q $ip_vm7 "lscpu |grep '^Model name' |awk -F ':' '{print \$2}' | awk '{\$1=\$1};1'")
	echo ${guest7_cpu_model}
else
	host_cpu_model=$(lscpu|grep "^Model name"|awk '{print $NF}')
	guest7_cpu_model=$(sshpass -p "$password" ssh -o StrictHostKeyChecking=no -q $ip_vm7 "lscpu |grep '^Model name' |awk '{print \$NF}'")
fi
echo "虚拟机vm7 CPU类型为：${guest7_cpu_model}"
echo ${res}
# 输出最终状态
[ "$res" = "0" ] && echo -e "\033[32m-----------------2.主机模式配置完成虚拟机$vm7_name能够正常启动，虚拟机CPU类型为：$guest7_cpu_model----------------\033[0m"
echo -e "\n\n\n"

cleanup_environment /dev/null 2>&1 
[ -e "$vm8_xml" ] && echo "已准备好测试xml" || { echo "请确认是否准备好xml"; exit 1; }
echo "3. 自定义模式虚拟机启动测试开始，虚拟机vm8关键配置如下："
cat $vm8_xml|grep "<cpu mode='custom' match='exact'>" || res=1
if [ ${arch} == "x86_64" ];then
	cat $vm8_xml|grep "<model>Dhyana</model>" || res=1
else
	cat $vm8_xml|grep "<model>Kunpeng-920</model>" || res=1
fi
cat $vm8_xml|grep "<topology sockets='2' dies='1' cores='2' threads='1'/>" || res=1
echo "开始执行命令：virsh define $vm8_xml"
virsh define $vm8_xml  > /dev/null 2>&1 || { res=1; echo "$vm8_name define failed"; exit 1; }
echo "开始执行命令：virsh start $vm8_xml"
virsh start "$vm8_name" > /dev/null 2>&1 || { res=1; echo "$vm8_name start failed"; exit 1; }
if [ $? -ne 0 ]; then
  echo "vm8 start failed."
fi
vnc_port8=$(virsh vncdisplay $vm8_name)
# 循环查询虚拟机的 IP 地址
echo "虚拟机vm8 启动中······"
while true; do
    ip_vm8=$(get_vm_ip "$vm8_name")
    # 如果有 IP 地址，打印信息并写入文件
    if [ -n "$ip_vm8" ]; then
        check_and_ping $ip_vm8 > /dev/null && echo "虚拟机$vm8_name 启动正常！" || { res=1; echo '虚拟机$vm8_name 启动失败'; }
        break  # 退出循环
    fi
    sleep 10
done

if [ ${arch} == "x86_64" ];then
	custom_cpu_model="Hygon Dhyana Processor"
	guest8_cpu_model=$(sshpass -p "$password" ssh -o StrictHostKeyChecking=no -q $ip_vm8 "lscpu |grep '^Model name' |awk -F ':' '{print \$2}' | awk '{\$1=\$1};1'")
else
	custom_cpu_model="Kunpeng-920"
	guest8_cpu_model=$(sshpass -p "$password" ssh -o StrictHostKeyChecking=no -q $ip_vm8 "lscpu |grep '^Model name' |awk '{print \$NF}'")
fi
echo "自定义CPU型号为：${custom_cpu_model}"
echo "虚拟机VM8 CPU型号为：${guest8_cpu_model}"
# 输出最终状态
[ "$custom_cpu_model" == "$guest8_cpu_model" ] && echo -e "\033[32m-----------------3.自定义模式配置完成虚拟机$vm8_name能够正常启动，虚拟机CPU类型与自定义CPU类型一致----------------\033[0m" || res=1
echo -e "\n\n\n"
# 判断用例是否执行成功
[ "$res" = "0" ] && echo -e "\033[32m-----------------test PASSED----------------\033[0m" || echo -e "\033[31m-----------------test FAILED----------------\033[0m"
